unit frmPaymentsList;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, frmCustomList, cxGraphics, cxControls, cxLookAndFeels,
  cxLookAndFeelPainters, cxContainer, cxEdit, dxSkinsCore,
  dxSkinOffice2007Black, cxStyles, dxSkinscxPCPainter, cxCustomData,
  cxFilter, cxData, cxDataStorage, DB, cxDBData, Uni, ActnList, DBAccess,
  MemDS, cxGridLevel, cxClasses, cxGridCustomView, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxGrid, AdvGlowButton, cxLabel,
  cxGroupBox, cxCalc, cxCalendar, cxDBLookupComboBox, DM, cxTextEdit,
  cxDropDownEdit, cxLookupEdit, cxDBLookupEdit, cxMaskEdit, cxCheckBox,
  dxmdaset;

type
  TPaymentsListFrame = class(TCustomListFrame)
    grdListViewid: TcxGridDBColumn;
    grdListViewnum: TcxGridDBColumn;
    grdListViewp_date: TcxGridDBColumn;
    grdListViewsumm: TcxGridDBColumn;
    grdListViewcurr_id: TcxGridDBColumn;
    grdListViewdescr: TcxGridDBColumn;
    grdListViewpayer_id: TcxGridDBColumn;
    grdListViewact_id: TcxGridDBColumn;
    grdListViewfull_summ: TcxGridDBColumn;
    grdListViewnb: TcxGridDBColumn;
    grdListViewaccaunt_id: TcxGridDBColumn;
    grdListViewparent_id: TcxGridDBColumn;
    grdListViewc_date: TcxGridDBColumn;
    grdListViewc_user: TcxGridDBColumn;
    quListid: TIntegerField;
    quListnum: TIntegerField;
    quListp_date: TDateField;
    quListsumm: TFloatField;
    quListcurr_id: TIntegerField;
    quListdescr: TStringField;
    quListpayer_id: TIntegerField;
    quListact_id: TIntegerField;
    quListfull_summ: TFloatField;
    quListnb: TIntegerField;
    quListaccaunt_id: TIntegerField;
    quListparent_id: TIntegerField;
    quListc_date: TDateField;
    quListc_user: TStringField;
    quListp_diff: TFloatField;
    grdListViewp_diff: TcxGridDBColumn;
    chkDate: TcxCheckBox;
    dtFrom: TcxDateEdit;
    dtTo: TcxDateEdit;
    cxLabel2: TcxLabel;
    lupClient: TcxDBLookupComboBox;
    memFilter: TdxMemData;
    memFilterid: TIntegerField;
    dsFilter: TDataSource;
    procedure actAddExecute(Sender: TObject);
    procedure actDelExecute(Sender: TObject);
    procedure actEditExecute(Sender: TObject);
    procedure grdListViewCustomDrawCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
      var ADone: Boolean);
    procedure chkDateClick(Sender: TObject);
    procedure lupClientPropertiesChange(Sender: TObject);
    procedure dtFromPropertiesChange(Sender: TObject);
    procedure dtToPropertiesChange(Sender: TObject);
  private
    { Private declarations }
    procedure DoFilter;
  public
    { Public declarations }
    constructor Create(AOwner: TComponent); override;    
    procedure RefreshData;
  end;

implementation

uses
  fmEnterPayment, DateUtils;
  
{$R *.dfm}

procedure TPaymentsListFrame.actAddExecute(Sender: TObject);
begin
  TEnterPaymentForm.ExecuteId(self);
  actRefr.Execute;
end;

procedure TPaymentsListFrame.actDelExecute(Sender: TObject);
begin
  inherited;
//  dmMain.PrepareForDeletePayment(quListid.Value);  
  quList.Delete;
end;

procedure TPaymentsListFrame.actEditExecute(Sender: TObject);
begin
  inherited;
  TEnterPaymentForm.ExecuteId(self, quListid.AsInteger);
  actRefr.Execute;
end;

constructor TPaymentsListFrame.Create(AOwner: TComponent);
begin
  inherited;
  FDataChanged := False;
  if MonthOf(now)/3 <= 1 then
    dtFrom.Date := StartOfTheYear(now)
  else if MonthOf(now)/3 <= 2 then
    dtFrom.Date := IncMonth(StartOfTheYear(now), 3)
  else if MonthOf(now)/3 <= 3 then
    dtFrom.Date := IncMonth(StartOfTheYear(now), 6)
  else if MonthOf(now)/3 <= 4 then
    dtFrom.Date := IncMonth(StartOfTheYear(now), 9);

  dtTo.Date := Trunc(Now);

  memFilter.Active := True;
  memFilter.Insert;
end;

procedure TPaymentsListFrame.RefreshData;
var
  s: string;
begin
  FDataChanged := False;
  actRefr.Execute;
  DoFilter;
end;

procedure TPaymentsListFrame.grdListViewCustomDrawCell(
  Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
  AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
  inherited;
  if (AViewInfo.Item.Index = grdListViewp_diff.Index)
    and not VarIsNull(AViewInfo.RecordViewInfo.GridRecord.Values[grdListViewp_diff.Index])
    and (AViewInfo.RecordViewInfo.GridRecord.Values[grdListViewp_diff.Index] > 0) then
    begin
      ACanvas.Font.Style := ACanvas.Font.Style + [fsBold];
      ACanvas.Font.Color := $0036A023;
    end;
end;

procedure TPaymentsListFrame.chkDateClick(Sender: TObject);
begin
  inherited;
  dtFrom.Enabled := chkDate.Checked;
  dtTo.Enabled := chkDate.Checked;
  DoFilter;
end;

procedure TPaymentsListFrame.lupClientPropertiesChange(Sender: TObject);
begin
  inherited;
  DoFilter;
end;

procedure TPaymentsListFrame.DoFilter;
var
  s: string;
begin
  dsList.DataSet.Filter := '';
  dsList.DataSet.Filtered := False;

  dsList.DataSet.Filter := '(parent_id is null) ';

  if not VarIsNull(lupClient.EditingValue) then
    dsList.DataSet.Filter := dsList.DataSet.Filter + ' and (payer_id = ' + VarToStr(lupClient.EditingValue) + ')';

  if chkDate.Checked then
  begin
    dsList.DataSet.Filter := dsList.DataSet.Filter + ' and ';
    s := '((p_date >= ''' + DateToStr(dtFrom.Date) + ''') and (p_date <= ''' + DateToStr(dtTo.Date) + '''))';
    dsList.DataSet.Filter := dsList.DataSet.Filter + s;
  end;

  if Length(dsList.DataSet.Filter) > 0 then
    dsList.DataSet.Filtered := True;
end;

procedure TPaymentsListFrame.dtFromPropertiesChange(Sender: TObject);
begin
  inherited;
  DoFilter;
end;

procedure TPaymentsListFrame.dtToPropertiesChange(Sender: TObject);
begin
  inherited;
  DoFilter;
end;

end.
